package com.example.yblin.hello24;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

import com.example.yblin.hello24.DataUtils.LocalDataUtil;
import com.example.yblin.hello24.DataUtils.LoginUtil;
import com.example.yblin.hello24.DataUtils.NetUtil;

public class WelcomeActivity extends AppCompatActivity {

    private MHandler handler;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_welcome);
        handler = new MHandler(this);
        new SignInAsync().execute();
    }


    public class SignInAsync extends AsyncTask<Void, Void, Integer> {
        @Override
        protected Integer doInBackground(Void... params) {
            return LoginUtil.login(WelcomeActivity.this);

        }

        @Override
        protected void onPostExecute(Integer result) {
            if (result == NetUtil.CODE_OK) {
                handler.sendEmptyMessageDelayed(MHandler.MSG_GO, 200);
            } else if (result == NetUtil.CODE_USERNAME_OR_PASSWORD_ERROR) {
                handler.sendEmptyMessageDelayed(MHandler.MSG_NEED_U_AND_P, 200);
            } else {
                handler.sendEmptyMessage(MHandler.MSG_ERROR);
            }
        }
    }

    class MHandler extends Handler {

        public static final int MSG_GO = 0;
        public static final int MSG_NEED_U_AND_P = 1;
        public static final int MSG_ERROR = 2;

        private Context context;

        public MHandler(Context context) {
            this.context = context;
        }

        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case MSG_GO:
                    context.startActivity(new Intent(context, MainActivity.class));
                    WelcomeActivity.this.finish();
                    break;
                case MSG_NEED_U_AND_P:
                    context.startActivity(new Intent(context, MainActivity.class));
                    context.startActivity(new Intent(context, LoginActivity.class));
                    WelcomeActivity.this.finish();
                    break;
                case MSG_ERROR:
                    AlertDialog.Builder builder = new AlertDialog.Builder(context);
                    builder.setTitle("网络异常")
                            .setMessage("请检查网络后重新启动！")
                            .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialogInterface, int i) {
                                    System.exit(0);
                                }
                            });
                    AlertDialog dialog = builder.create();
                    dialog.show();
                    break;
            }
        }
    }
}